package cn.yllr.tomgxianghui.controller;


import cn.yllr.tomgxianghui.domain.Order;
import cn.yllr.tomgxianghui.entity.User;
import cn.yllr.tomgxianghui.model.PartOrderDetail;
import cn.yllr.tomgxianghui.service.OrderService;
import cn.yllr.tomgxianghui.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@Slf4j
public class OrderController {

    @Resource
    private OrderService service;

    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("/addOrder.do")
    @ResponseBody
    public String addOrder(String address, Integer price, HttpSession session, String put) {
        Boolean flag = service.addorder(String.valueOf(session.getAttribute("username")), address, price, put);
        if (flag) {
            return "1";
        } else {
            return "0";
        }

    }

    /**
     * 展示用户所有订单
     *
     * @param model
     * @return
     */
    @RequestMapping("/user_order.html")
    public String findOrder(Model model,HttpSession session) {
        List<Order> list = service.findOrderByCookid((String) session.getAttribute("username"));
        model.addAttribute("lists", list);
        return "user_center/user_order";
    }

    /**
     * 根据id查找订单,用来展示订单详情
     *
     * @param oid
     * @return
     */
    @RequestMapping("/findOrderByCid.do")
    public String findOrderByCid(String oid, Model model, HttpSession session) {
        Order order = service.findOrderByCid(oid);
        Long expire = template.opsForValue().getOperations().getExpire(oid);
        System.out.println("expire = " + expire);
        model.addAttribute("order", order);              //订单信息展示给页面
        model.addAttribute("redistime", expire);           //订单时长展示给页面
//        System.out.println(order);
        List<PartOrderDetail> OrderDetails = service.findorderDetail(oid);
        model.addAttribute("detail_orders", OrderDetails);//查找订单内的详细商品
        User user = service.findUserPhoneBysession(String.valueOf(session.getAttribute("username")));      //查找用户手机号码
        model.addAttribute("user", user);
        return "user_center/order_detail";
    }

    /**
     * 根据id删除订单
     *
     * @param oid
     * @return
     */
    @RequestMapping("/deleteOrder")
    public String deleteOrder(String oid) {
        int i = service.deleteOrders(oid);
        System.out.println("i = " + i);
        return "redirect:/user_order.html";
    }

    /**
     * 取消订单
     *
     * @param orderid
     * @return
     */
    @RequestMapping("/redisoverdue")
    public String cancelOrder(String orderid) {
        redisUtil.delete(orderid);
        service.cancelOrderByid(orderid);
        return "forward:/user_order.html";
    }

    /**
     * 展示订单信息到支付页面
     * @param orderid
     * @param model
     * @return
     */
    @RequestMapping("/user_pay.html")
    public String paymentshow(String orderid,Model model){
        Order orderByCid = service.findOrderByCid(orderid);
        model.addAttribute("payorder",orderByCid);
        return "user_center/user_pay";
    }

    /**
     * 顾客支付
     * @param orderid
     * @return
     */
    @RequestMapping("/weixinpay")
    @ResponseBody
    public String payOrder(String orderid){
        int i = service.updatePayStatus(orderid);
        if(i==1){
            return "1";
        }else {
            return "0";
        }
    }

    @RequestMapping("/testredis")
    @ResponseBody
    public void testredis() {
        redisUtil.set("71", "dd", 30);
    }

    @Resource
    private RedisTemplate<String, Object> template;

    @RequestMapping("/testo")
    @ResponseBody
    public void testoutput() {
        Long expire = template.opsForValue().getOperations().getExpire("71");
        System.out.println("expire = " + expire);
    }
}
